package com.woniuxy.oldmanapi.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.oldmanapi.entity.Care;
import com.woniuxy.oldmanapi.entity.Changecare;
import com.woniuxy.oldmanapi.entity.Older;
import com.woniuxy.oldmanapi.mapper.CareMapper;
import com.woniuxy.oldmanapi.mapper.ChangecareMapper;
import com.woniuxy.oldmanapi.mapper.OlderMapper;
import com.woniuxy.oldmanapi.yvo.ChangeCareVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChangeCareService {
    @Autowired
    ChangecareMapper changecareMapper;
    @Autowired
    OlderMapper olderMapper;
    @Autowired
    CareMapper careMapper;

    public PageInfo<ChangeCareVo> selectAllListYsw(String name, Integer now, Integer size) {
        PageHelper.startPage(now, size);
        List<ChangeCareVo> arr = changecareMapper.selectAllListYsw(name);
        return new PageInfo<>(arr);
    }

    public Care selectOlderCareIdById(int id) {
        Older older = olderMapper.selectByPrimaryKey(id);
        Care care = careMapper.selectByPrimaryKey(older.getCareId());
        return care;
    }

    public void addChange(Changecare changecare) {
        //更换长者信息记录
        olderMapper.updateCareById(changecare.getOlderId(), changecare.getNewcareId());
        //添加信息
        changecareMapper.deleteByOlderId(changecare.getOlderId());
        changecareMapper.insert(changecare);
    }

    public void deleteChange(int id) {
        changecareMapper.deleteByPrimaryKey(id);
    }
}
